#!/usr/bin/env bash

INSTANCE=""
OUTFILE=playlist.json
TOKEN=""
while getopts "i:o:t:" arg; do
	case $arg in
		i)
			INSTANCE=${OPTARG}
			;;
		o)
			OUTFILE=${OPTARG}
			;;
		t)
			TOKEN=${OPTARG}
			;;
	esac
done
shift $((OPTIND-1))

if [ -z "${INSTANCE}" ]; then
    echo No instance given
	exit 1
fi

if [ -z "${TOKEN}" ]; then
    echo No token given
	exit 1
fi

if [ "$(host ${INSTANCE}|grep "not found"|wc -l)" -lt 0 ]; then
	echo Instance ${INSTANCE} has no DNS entry
	exit 1
fi

if [ "$(curl -s -X 'GET' https://${INSTANCE}/api/v1/instance/nodeinfo/2.0/ -H 'accept: application/json' |jq -r '.software.name')" != "funkwhale" ]; then
	echo Instance ${INSTANCE} seems not to be a funkwhale instance
	exit 1
fi

for plid in $(find . -name "*pl.json"|xargs -n1 jq -r '.|[.name,.id]|@csv' |sort|cut -d',' -f2); do
	plname="$(jq -r '.name' ${plid}.pl.json)"
	plurl=$(printf "${plname}" | jq --slurp --raw-input --raw-output @uri)
	echo $plurl
	answer=$(curl -s -X 'GET' https://${INSTANCE}/api/v1/playlists/?name=${plurl} -H 'accept: application/json' -H 'Authorization: Bearer '${TOKEN})
	if [ "$(echo ${answer}|jq -r '.count // 0')" -eq 0 ]; then
		valuejson=$(jq -c --null-input --arg plname "${plname}" '{"name":$plname,"privacy_level":"instance"}')
		curl -s -X 'POST' https://${INSTANCE}/api/v1/playlists/ -H 'Content-Type: application/json' -H 'accept: application/json' -H 'Authorization: Bearer '${TOKEN} -d "${valuejson}"
	fi
	answer=$(curl -s -X 'GET' https://${INSTANCE}/api/v1/playlists/?name=${plurl} -H 'accept: application/json' -H 'Authorization: Bearer '${TOKEN})
	if [ "$(echo ${answer}|jq -r '.count')" -eq 1 ]; then
		plidt=$(echo ${answer}|jq -r '.results[0].id')
#		echo $plidt
		tracklist=""
		while IFS='#' read mbid title; do
#			echo $mbid
			if [ ! -z "${mbid}" ]; then
#			if [ "${mbid}" != "null" ]; then
				answer=$(curl -s -X 'GET' https://${INSTANCE}/api/v1/tracks/?mbid=${mbid} -H 'accept: application/json' -H 'Authorization: Bearer '${TOKEN})
#				echo $answer
				stitle=$(echo ${answer}|jq -r '.results[0].title')
				id=$(echo ${answer}|jq -r '.results[0].id')
				if [ "${id}" != "null" ]; then
					tracklist="${tracklist},${id}"
				fi
			else
				titleurl=$(printf "${title}" | jq --slurp --raw-input --raw-output @uri)
				answer=$(curl -s -X 'GET' https://${INSTANCE}/api/v1/tracks/?title=${titleurl} -H 'accept: application/json' -H 'Authorization: Bearer '${TOKEN})
				if [ "$(echo ${answer}|jq -r '.count')" -eq 1 ]; then
#					echo $answer
					id=$(echo ${answer}|jq -r '.results[0].id')
					tracklist="${tracklist},${id}"
				fi
			fi
#			echo ${mbid} - ${title} - ${stitle} - ${id}
		done <<<$(jq -r '.results[].track|[.mbid,.title]|@csv' ${plid}.tracks.json |sed -e 's/","/"#"/g;s/^,/#/g'|tr -d '"')
		tracklist=$(echo ${tracklist}|sed -e 's/^,//g')
		valuejson="{\"tracks\": [$tracklist],\"allow_duplicates\": true}"
		valuejson=$(echo $valuejson|jq -rc)
		curl -s -X 'POST' https://${INSTANCE}/api/v1/playlists/${plidt}/add/ -H 'Content-Type: application/json' -H 'accept: application/json' -H 'Authorization: Bearer '${TOKEN} -d "${valuejson}"
		
	fi
done
